<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">领取时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input" type="text"
                                               placeholder="选择时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startTime" id="startTime" type="hidden"/>
                                    <input name="endTime" id="endTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">物料名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuName" class="layui-input" type="text" placeholder="请输入物料名称" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">物料编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" autocomplete="off"/>
                                        <!--<select name="skuCode" lay-filter="skuCode" lay-search lay-verify="">
                                            <option value="">请选物料编号</option>
                                        </select>-->
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">物料唯一码：</label>
                                    <div class="layui-input-inline">
                                        <input name="labelCode" class="layui-input" type="text" placeholder="请输入物料唯一码" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">包装码：</label>
                                    <div class="layui-input-inline">
                                        <input name="parentCode" class="layui-input" type="text" placeholder="请输入包装码" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">工单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="workOrderCode" class="layui-input" type="text" placeholder="请输入工单编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">领新订单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入领新订单编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <!--业务类型:cellCabOut(1,售刀柜领取);cellCabOutByOldNew(2,售刀柜领取(以旧换新))-->
                                <div class="layui-inline">
                                    <label class="layui-form-label">领新类型：</label>
                                    <div class="layui-input-inline">
                                        <select name="businessType" lay-filter="businessType" lay-search lay-verify="">
                                            <option value="">请选领新类型</option>
                                            <option value="cellCabOut">售刀柜领新</option>
                                            <option value="outByCabSpace">存刀柜领新</option>
                                            <option value="cellCabOutByOldNew">售刀柜领取(以旧换新)</option>
                                            <!--<option value="outByCabSpace">格子柜领新(以旧换新)</option>-->
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">部门：</label>
                                    <div class="layui-input-inline">
                                        <select name="departmentId" lay-filter="departmentId" lay-search lay-verify="">
                                            <option value="">请选部门</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">岗位角色：</label>
                                    <div class="layui-input-inline">
                                        <select name="roleId" lay-filter="roleId" lay-search lay-verify="">
                                            <option value="">请选岗位角色</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">主柜编号：</label>
                                    <div class="layui-input-inline">
                                        <select name="mainCabCode" lay-filter="cabCode" lay-search lay-verify="">
                                            <option value="">请选主柜编号</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">辅柜编号：</label>
                                    <div class="layui-input-inline">
                                        <select name="cabCode" lay-filter="cabCode" lay-search lay-verify="">
                                            <option value="">请选辅柜编号</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">机台：</label>
                                    <div class="layui-input-inline">
                                        <select name="deviceCode4Query" lay-filter="deviceCode" lay-search
                                                lay-verify="">
                                            <option value="">请选择机台</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">领取人：</label>
                                    <div class="layui-input-inline">
                                        <select id="creatorId" name="creatorId" lay-filter="creatorId" lay-search
                                                lay-verify="">
                                            <option value="">请选领取人</option>
                                        </select>
                                    </div>
                                </div>

                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">

                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="cellInventoryOutLabelTable" lay-filter="cellInventoryOutLabelTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="cellInventoryOutLabelTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="cellInventoryOutLabelForm">
    <form lay-filter="cellInventoryOutLabelForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改者</label>
            <div class="layui-input-block">
                <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block">
                <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-block">
                <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">领新条码明细编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入领新条码明细编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">领新id</label>
            <div class="layui-input-block">
                <input name="inoutId" placeholder="请输入领新id" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">领新明细id</label>
            <div class="layui-input-block">
                <input name="inoutDetailId" placeholder="请输入领新明细id" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物料唯一码</label>
            <div class="layui-input-block">
                <input name="labelCode" placeholder="请输入物料唯一码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">包装条码</label>
            <div class="layui-input-block">
                <input name="parentCode" placeholder="请输入包装条码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="cellInventoryOutLabelFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    let dateValue = value.split(" - ");
                    $("#startTime").val(dateValue[0] + " 00:00:00");
                    $("#endTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }
            }
        });

        // 获取部门
        admin.req('user/admin/department/list', {limit: 999}, function (res) {
            if (200 == res.code) {
                var optionHtml = '<option value="">请选择部门</option>';
                for (var k = 0; k < res.data.length; k++) {
                    optionHtml += '<option value="' + res.data[k].id + '">' + res.data[k].name + '</option>';
                }
                $("select[name='departmentId']").html(optionHtml);
                form.render('select');

            } else {
                layer.msg('获取部门失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        // 获取岗位角色
        admin.req('user/admin/role/list', {limit: 999}, function (res) {
            if (200 == res.code) {
                var optionHtml = '<option value="">请选择岗位角色</option>';
                for (var k = 0; k < res.data.length; k++) {
                    optionHtml += '<option value="' + res.data[k].id + '">' + res.data[k].name + '</option>';
                }
                $("select[name='roleId']").html(optionHtml);
                form.render('select');

            } else {
                layer.msg('获取岗位失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        var selectManager = function (departmentId, elementId, optionDesc) {
            //获取责任人erpDepartmentId
            admin.req('user/admin/user/list', {departmentId: departmentId, limit: 999}, function (res) {
                if (200 == res.code) {
                    var optionHtml = '<option value="">请选择' + optionDesc + '</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        optionHtml += '<option value="' + res.data[k].id + '">' + res.data[k].nickname + '|' + res.data[k].username + '</option>';
                    }
                    $(elementId).html(optionHtml);
                    form.render('select');


                } else {
                    layer.msg('获取责任人失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }
        selectManager(undefined, '#creatorId', '领取人');

        form.on('select(departmentId)', function (data) {
            selectManager(data.value, '#creatorId', '领取人');
        })

        admin.req('enterprisecab/cabinet/admin/cabinet/list', {
            limit: 999,
            cabinetNature: 'main',
            cabinetType: 'spring'
        }, function (res) {
            if (200 == res.code) {
                var optionHtml = '<option value="">请选择主柜</option>';
                for (var k = 0; k < res.data.length; k++) {
                    console.log(res.data[k]);
                    var nameCode = '';
                    if (res.data[k].cabName) {
                        nameCode = res.data[k].cabName + '|' + res.data[k].cabCode;
                    } else {
                        nameCode = res.data[k].cabCode;
                    }
                    optionHtml += '<option value="' + res.data[k].cabCode + '">' + nameCode + '</option>';
                }
                $("select[name='mainCabCode']").html(optionHtml);
                form.render('select')
            } else {
                layer.msg(res.msg, {icon: 2, time: 1000});
            }
        }, 'GET');

        admin.req('enterprisecab/cabinet/admin/cabinet/list', {
            limit: 999,
            cabinetNature: 'sale',
            cabinetType: 'spring'
        }, function (res) {
            if (200 == res.code) {
                var optionHtml = '<option value="">请选择辅柜</option>';
                for (var k = 0; k < res.data.length; k++) {
                    var nameCode = '';
                    if (res.data[k].cabName) {
                        nameCode = res.data[k].cabName + '|' + res.data[k].code;
                    } else {
                        nameCode = res.data[k].code;
                    }
                    optionHtml += '<option value="' + res.data[k].code + '">' + nameCode + '</option>';
                }
                $("select[name='cabCode']").html(optionHtml);
                form.render('select')
            } else {
                layer.msg(res.msg, {icon: 2, time: 1000});
            }
        }, 'GET');

        admin.req('enterprise/device/admin/device/list', {limit: 999}, function (res) {
            if (200 == res.code) {
                var optionHtml = '<option value="">请选择机台</option>';
                for (var k = 0; k < res.data.length; k++) {
                    var nameCode = '';
                    if (res.data[k].name) {
                        nameCode = res.data[k].name + '|' + res.data[k].deviceCode;
                    } else {
                        nameCode = res.data[k].deviceCode;
                    }
                    optionHtml += '<option value="' + res.data[k].deviceCode + '">' + nameCode + '</option>';
                }
                $("select[name='deviceCode4Query']").html(optionHtml);
                form.render('select')
            } else {
                layer.msg(res.msg, {icon: 2, time: 1000});
            }
        }, 'GET');

        // 渲染表格
        var insTb = table.render({
            elem: '#cellInventoryOutLabelTable',
            url: config.base + 'enterprisecab/cabinet/admin/cellInventoryOutLabel/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'labelCode', title: '物料唯一码'}
                ,{field: 'businessTypeDesc', title: '领新类型', width: 120}
                ,{field: 'parentCode', title: '所属包装码'}
                ,{field: 'packName', title: '包装规格'}
                ,{field: 'price', title: '包装单价'}
                ,{field: 'basePrice', title: '基础单价'}
                ,{field: 'code', title: '领新订单编号'}
                , {
                    field: 'skuPic', title: '物料图片', templet: function (d) {
                        if (d.skuPic) {
                            return '<img src="' + d.skuPic + '" style="height: 20px;"/>';
                        }
                        return '<img src="assets/images/default-icon.png"/>';
                    }
                }
                , {field: 'skuName', title: '物料名称'}
                , {field: 'skuCode', title: '物料编号'}
                , {field: 'skuSpecOld', title: '规格码', width: 160}
                , {field: 'productBrandName', title: '品牌'}
                , {field: 'mainCabCode', title: '主柜编号'}
                , {field: 'cabCode', title: '辅柜编号'}
                , {field: 'spaceCode', title: '货道编号'}
                , {field: 'workOrderCode', sort: false, title: '工单'}
                , {field: 'deviceCode', sort: false, title: '机台'}
                , {field: 'deviceDepartmentName', sort: false, title: '机台部门'}
                , {field: 'quantityPack', title: '数量(基础单位计)', width: 140, templet: function (d) {
                        return d.quantityPack < 0 ? d.quantityPack * -1 : d.quantityPack
                    }}
                , {field: 'basicsUnitName', title: '基础单位'}
                , {field: 'creatorName', title: '领取人'}
                , {field: 'departmentName', title: '领取人部门'}
                , {field: 'roleName', title: '领取人岗位'}
                ,{field: 'createTime', title: '领取时间', width: 170}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(cellInventoryOutLabelTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(cellInventoryOutLabelTable)', function(obj){
            table.reload('cellInventoryOutLabelTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for(let key in field){
                field[key] = ''
            }
            insTb.reload({where: field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#cellInventoryOutLabelBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '680px',
                offset: '65px',
                title: data ? '修改领新条码' : '添加领新条码',
                content: $('#cellInventoryOutLabelForm').html(),
                success: function () {
                    form.val('cellInventoryOutLabelForm', data);
                    // 表单提交事件
                    form.on('submit(cellInventoryOutLabelFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprisecab/cabinet/admin/cellInventoryOutLabel/update' : 'enterprisecab/cabinet/admin/cellInventoryOutLabel/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('cellInventoryOutLabelTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/cabinet/admin/cellInventoryOutLabel/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#cellInventoryOutLabelBtnExport').click(function () {
            var checkRows = table.checkStatus('cellInventoryOutLabelTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>